package com.bienvenue.main.ui.activity.personal;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import com.bienvenue.main.R;
import com.bienvenue.main.base.BaseActivity;
import com.bienvenue.main.httpService.ResponseObserver;
import com.bienvenue.main.model.HttpBean;
import com.bienvenue.main.model.ResumeInfoModel;
import com.bienvenue.main.model.SaveResumeModel;
import com.bienvenue.main.serviceApi.ServiceModel;
import com.bienvenue.main.utils.SPUtils;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 　　　　　　　 ┏┓       ┏┓+ +
 * 　　　　　　　┏┛┻━━━━━━━┛┻┓ + +
 * 　　　　　　　┃　　　　　　 ┃
 * 　　　　　　　┃　　　━　　　┃ ++ + + +
 * 　　　　　　 █████━█████  ┃+
 * 　　　　　　　┃　　　　　　 ┃ +
 * 　　　　　　　┃　　　┻　　　┃
 * 　　　　　　　┃　　　　　　 ┃ + +
 * 　　　　　　　┗━━┓　　　 ┏━┛
 * ┃　　  ┃
 * 　　　　　　　　　┃　　  ┃ + + + +
 * 　　　　　　　　　┃　　　┃　Code is far away from     bug with the animal protecting
 * 　　　　　　　　　┃　　　┃ + 　　　　         神兽保佑,代码无bug
 * 　　　　　　　　　┃　　　┃
 * 　　　　　　　　　┃　　　┃　　+
 * 　　　　　　　　　┃　 　 ┗━━━┓ + +
 * 　　　　　　　　　┃ 　　　　　┣┓
 * 　　　　　　　　　┃ 　　　　　┏┛
 * 　　　　　　　　　┗┓┓┏━━━┳┓┏┛ + + + +
 * 　　　　　　　　　 ┃┫┫　 ┃┫┫
 * 　　　　　　　　　 ┗┻┛　 ┗┻┛+ + + +
 *
 * @ProjectName: Bienvenue
 * @Package: com.bienvenue.main.ui.activity
 * @ClassName: WorkExperienceActivity
 * @Description: 工作经历
 * @Author: Mr.Chen
 * @UpdateUser: Mr.Chen
 * @UpdateDate: 2020/9/16
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class WorkExperienceActivity extends BaseActivity {
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.et_prompt_name)
    EditText etPromptName;
    @BindView(R.id.et_work_position)
    EditText etWorkPosition;
    @BindView(R.id.et_work_description)
    EditText etWorkDescription;
    @BindView(R.id.tv_delete_btn)
    TextView tvDeleteBtn;

    @BindView(R.id.et_job_details)
    EditText etJobDetails;
    int type;
    String userId;
    ResumeInfoModel.DataBean.UResumeWorkBean bean;

    @Override
    protected void initData() {
        userId = SPUtils.getString("userId");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getInt("type");
            bean = bundle.getParcelable("workExperience");
            if (type == 1) {
                tvDeleteBtn.setVisibility(View.VISIBLE);
                etPromptName.setText(bean.getCompanyname());
                String time = bean.getStartendtime();
                String startTime = time.substring(0, time.indexOf("~"));
                String endTime = time.substring(time.indexOf("~"));
                tvStartTime.setText(startTime);
                tvEndTime.setText(endTime);
                etWorkPosition.setText(bean.getJobtitle());
                etWorkDescription.setText(bean.getSalary());
                etJobDetails.setText(bean.getDescription());
            } else {
                tvDeleteBtn.setVisibility(View.GONE);
            }
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_word_experience;
    }

    @OnClick(R.id.iv_return_btn)
    void onFinish() {
        this.finish();
    }

    @OnClick(R.id.tv_start_time)
    void onStartTime() {

        Calendar selectedDate = Calendar.getInstance();//系统当前时间

        Calendar startDate = Calendar.getInstance();
        startDate.set(1927, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2022, 12, 31);
        TimePickerView timePicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {

                tvStartTime.setText(getTime(date));
            }
        })
                .setTitleText("请选择开始时间")
                .setGravity(Gravity.BOTTOM)
                .setContentTextSize(18)
                .setTitleSize(20)
                .setDate(selectedDate)
                .setSubmitText("确定")
                .setCancelText("取消")
                .setRangDate(startDate, endDate)
                .build();
        timePicker.show();
    }

    @OnClick(R.id.tv_end_time)
    void onEndTime() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间

        Calendar startDate = Calendar.getInstance();
        startDate.set(1927, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2022, 12, 31);
        TimePickerView timePicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                tvEndTime.setText(getTime(date));

            }
        })
                .setTitleText("请选择结束时间")
                .setGravity(Gravity.BOTTOM)
                .setContentTextSize(18)
                .setTitleSize(20)
                .setDate(selectedDate)
                .setSubmitText("确定")
                .setCancelText("取消")
                .setRangDate(startDate, endDate)
                .build();
        timePicker.show();
    }

    @OnClick(R.id.tv_save_btn)
    void onSave() {

        if (TextUtils.isEmpty(etPromptName.getText().toString().trim()) && etPromptName.getText().toString().trim().equals("")) {
            showToast("请输入公司名称");
            return;
        } else if (tvStartTime.getText().toString().trim().equals("开始时间")) {
            showToast("请选择工作开始的时间");
            return;
        } else if (tvEndTime.getText().toString().trim().equals("结束时间")) {
            showToast("请选择工作结束的时间");
            return;
        } else if (TextUtils.isEmpty(etWorkPosition.getText().toString().trim()) && etWorkPosition.getText().toString().trim().equals("")) {
            showToast("请输入岗位名称");
            return;
        } else if (TextUtils.isEmpty(etWorkDescription.getText().toString().trim()) && etWorkDescription.getText().toString().trim().equals("")) {
            showToast("请输入工作薪资");
            return;

        } else if (TextUtils.isEmpty(etJobDetails.getText().toString().trim()) && etJobDetails.getText().toString().trim().equals("")) {
            showToast("请输入职责描述");
        } else if (TextUtils.isEmpty(userId)) {
            showToast("信息错误，请重新登录");
        }

        SaveResumeModel.WorkYearsModel model = new SaveResumeModel.WorkYearsModel();
        model.setCompanyname(etPromptName.getText().toString().trim());
        model.setDescription(etJobDetails.getText().toString().trim());
        model.setJobtitle(etWorkPosition.getText().toString().trim());
        model.setSalary(etWorkDescription.getText().toString().trim());
        model.setStartendtime(tvStartTime.getText().toString().trim() + "~" + tvEndTime.getText().toString().trim());
        model.setUserid(userId);
        model.setId(bean.getId());

        ServiceModel.getServiceModel().onSaveResumeWorkingYears(model, new ResponseObserver<HttpBean>() {
            @Override
            public void onSuccess(HttpBean response) {
                if (response.getCode() == 200) {
                    showToast(response.getMsg());
                    WorkExperienceActivity.this.finish();
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                showLog(message);
            }
        });

    }

    @OnClick(R.id.tv_delete_btn)
    void onDelete() {
        ServiceModel.getServiceModel().deleteResumeWork(bean.getId(), new ResponseObserver<HttpBean>() {
            @Override
            public void onSuccess(HttpBean response) {
                if (response.getCode() == 200) {
                    showToast(response.getMsg());
                    WorkExperienceActivity.this.finish();
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                showLog(message);

            }
        });

    }


}
